// pages/user_end/leave_comment/leave_comment.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: ['不想要了', '发货时间太长'],
    index: '',
    show: true,
    active: 1,
    rate:5,
    content:"",
    id:0,
    order:[],
    imgs:[],
    picList:[],
    token:'',
    star:5,
	captcha:"",
	countdown: 60,
	isDisabled: false,
	btnText:"发送验证码"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    that.setData({
       id:options.id
    })
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token:res.token
          })
          that.getOrder();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
    // 倒计时函数
    countdown: function() {
      let that = this;
      let countdown = that.data.countdown;
      
      // 设置定时器
      let timer = setInterval(function() {
        countdown--;
        that.setData({
          btnText: countdown + '秒后重试'
        });
        
        // 当倒计时结束时
        if (countdown <= 0) {
          clearInterval(timer);
          that.setData({
            btnText: '发送验证码',
            isDisabled: false
          });
        }
      }, 1000);
    },

  getOrder: function() {
    var that = this;
    let param = {
        id:that.data.id,
        token:that.data.token
    }
    wx.showNavigationBarLoading();  //在当前页面显示导航条加载动画
    request.request(url.orderdetail, param, url.REQUEST_METHOD_GET).then((res) => {
          wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
          that.setData({
              order:res.data.data
          })
          if(res.data.data.bargain==1){
            //议价
            that.setData({
              index:2,
              cost:''
            })
          }else{
            that.setData({
              index:1
            })
          }
    })

  },
   // 删除图片
 deleteImg: function (e) {
  var imgs = this.data.imgs;
  var picList = this.data.picList;
  var index = e.currentTarget.dataset.index;
  imgs.splice(index, 1);
  picList.splice(index, 1);
   this.setData({
         imgs: imgs,
         picList:picList
 });
 console.log(picList);
 },
 chooseImg: function (e) {
    var that = this;
    var imgs = this.data.imgs;
    if (imgs.length >= 3) {
      this.setData({
        lenMore: 1
       });
      setTimeout(function () {
        that.setData({
           lenMore: 0
           });
          }, 2500);
         return false;
    }
    var count = 3 -imgs.length;
    if(count< 1){
      return false;
    }

    wx.chooseImage({
      count: count, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          if (res.errMsg == "chooseImage:ok") {
            wx.showLoading({  //显示loding提示框
              title: '正在上传中...',
            })
            let picList = that.data.picList
            var imgs = that.data.imgs;
            res.tempFilePaths.forEach((item, index) => {
              imgs.push(item)
              that.setData({
                imgs: imgs,
               });
              wx.uploadFile({
                url:  url.HttpServerHostAddress+url.uploadpic,
                filePath: item,
                name: 'file',
                formData: {
                  token: that.data.token
                },
                success: function(res) {
                  let ress = JSON.parse(res.data)
                  if (ress.code == 1) {
                      var picList = that.data.picList;
                      picList.push(JSON.parse(res.data).data.url);
                      that.setData({
                        picList:picList,
                     })
                     console.log(picList);
                  }  else{
                           wx.showToast({
                              title: JSON.parse(res.data).msg,
                              icon: 'none',
                              duration: 3000
                         })
                       return;  } 
                },
                fail: res => {
                  console.log(res)
                }
              })
            })
            wx.hideLoading();  //隐藏loading提示框
          }
     }
    }); 
 },
 changecontent:function(e){
   this.setData({
     content:e.detail.value
   })
 },
 onChange(e){
   this.setData({
     star:e.detail,
     rate:e.detail
   })
 },
 commentOrder:function(e){
	var orderid = e.currentTarget.dataset.id;
	
  var that = this;
  if(!that.data.content){
     wx.showToast({title:"请输入异常原因！",icon:"none"});
     return false;
   }
   let param = {
      id:that.data.id,
      token:that.data.token,
      rate :that.data.rate,
      content:that.data.content,
      images:that.data.picList,
	  flag:1
    }
    wx.showNavigationBarLoading();  //在当前页面显示导航条加载动画
    request.request(url.commentorder, param, url.REQUEST_METHOD_POST).then((res) => {
          wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
          if(res.data.code==1){
			  
            setTimeout(function(){
                wx.navigateBack({
                  delta:1
                })							
            },1500)
            return false;
           }  
    })
 },
 acceptorder(id){
 	// var id = e.currentTarget.dataset.id;
 	var that = this;
 	let param = {
 	  scoure: 'wxapp',
 	  token:that.data.token,
 	  id:id
 	}
 	wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
 	request.request(url.acceptorder, param, url.REQUEST_METHOD_GET).then((res) => {
 	  wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
 	  wx.showToast({
 	    title: res.data.msg,
 	    icon:"none"
 	  })
 	  if(res.data.code==1){
 	     that.setData({
 	        page:1,
 	     })
 	      that.getOrdernum();
 	     that.getOrder();
 	   }
 	})  	
 },
 smssend(){
 	// var id = e.currentTarget.dataset.id;
	
 	var that = this;
	if(that.data.isDisabled==true)
	{
		wx.showToast({title:"请稍后再试！",icon:"none"});
		return false;
	}
	 this.setData({
	      isDisabled: true
	    });
    this.countdown();
	console.log('isDisabled：'+that.data.isDisabled);
		
	const mobile = that.data.order.worker.contact_tel;
 	let param = {
 	  scoure: 'wxapp',
 	  token:that.data.token,
 	  mobile:'13568996287',
	   event:'notice',
 	}
 	wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
 	request.request(url.smssend, param, url.REQUEST_METHOD_POST).then((res) => {
 	  wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
 	  wx.showToast({
 	    title: res.data.msg,
 	    icon:"none"
 	  })
 	  if(res.data.code==1){
 	     that.setData({
 	        page:1,
 	     })
 	   }
 	})  	
 },
 smscheck(id){
 	// var id = e.currentTarget.dataset.id;
 	var that = this;
 	let param = {
 	  scoure: 'wxapp',
 	  token:that.data.token,
 	  mobile:'13568996287',
	  event:'notice',
	  captcha:''
 	}
 	wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
 	request.request(url.smscheck, param, url.REQUEST_METHOD_GET).then((res) => {
 	  wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
 	  wx.showToast({
 	    title: res.data.msg,
 	    icon:"none"
 	  })
 	  if(res.data.code==1){
 	     that.setData({
 	        page:1,
 	     })
 	   }
 	})  	
 }
})